#import "OmMineVCButtonsView.h"
@interface OmMineVCButtonsView ()
@property (nonatomic, strong) NSMutableArray<UIButton *> *buttons;
@end
@implementation OmMineVCButtonsView {
    NSArray<NSDictionary *> *_settings;
}
- (instancetype)initWithSettings:(NSArray<NSDictionary *> *)settings {
    self = [super init];
    if (self) {
        _settings = settings;
        self.buttons = [NSMutableArray array];
        [self configSelf];
        [self allocateComponents];
        [self layoutComponents];
    }
    return self;
}
- (void)configSelf {
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(WINDOW_WIDTH));
    }];
    self.backgroundColor = [UIColor whiteColor];
}
- (void)allocateComponents {
    for (NSDictionary *buttonInformation in _settings) {
        NSString *title = buttonInformation[@"title"];
        SEL selector = NSSelectorFromString(buttonInformation[@"selector"]);
        OmBaseButton *button = [[OmBaseButton alloc] initWithImageName:@"OmRightIndex"
                                                             imageSize:CGSizeMake(RPX(7), RPX(12))
                                                    imageRightDistance:RPX(25)
                                                                 title:title
                                                             titleFont:RPXFont(14)
                                                            titleColor:[UIColor blackColor]
                                                     titleLeftDistance:RPX(25)];
        button.fp_addTargetActionForControlEvents(self.viewController, selector, UIControlEventTouchUpInside);
        if (_settings.lastObject != buttonInformation) {
            UIView *line = [[UIView alloc] init].fp_backgroundColor(HexadecimalColor(@"#F2F2F2"));
            [button addSubview:line];
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.left.bottom.equalTo(button);
                make.height.equalTo(@(1));
            }];
        }
        [self.buttons addObject:button];
        [self addSubview:button];
    }
}
- (void)layoutComponents {
    for (int i = 0; i < _buttons.count; i++) {
        UIButton *currentButton = _buttons[i];
        UIButton *lastButton = _buttons[i-1];
        [currentButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@(WINDOW_WIDTH));
            make.height.equalTo(@(RPX(45)));
            if (i == 0) {
                make.top.equalTo(self);
            } else {
                make.top.equalTo(lastButton.mas_bottom);
            }
            if (i == (self->_buttons.count - 1)) {
                make.bottom.equalTo(self);
            }
        }];
    }
}
@end
